<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/21/021
 * Time: 15:05
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\UserBase;
use app\Lib\Model\Push\WeiXinPush;
use app\Lib\Model\Role\GetRoleUser;
use app\Lib\Model\User\User;
use think\Url;

class OrderPush
{
    private  $order;
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * 业主下单推送
     * @return bool
     * @throws BlException
     */
    public function addOrder(){
        $phone =  $this->order->get_user_phone();
        $user   = new UserBase();
        $weiXin = $user->where([$user->_mobilephone=>$phone])->value($user->_id);
        $pushUser = new User($weiXin);
        if(!$pushUser->getWeiXin() || $pushUser->getWeiXin() == '无'){
            return true;
        }
        $firstValue = '尊敬的'.$pushUser->getNickName().'业主,您好。您在我们公司下单成功，阳新帝豪装饰将竭力为您服务。';
        $keyword2 = $this->order->get_user_address();
        $keyword3 = $this->order->get_style_obj()->getTagName();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],
            "keyword1"=>[
                'value'=>'阳新',
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>$keyword2,
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>$keyword3,
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'阳新帝豪全体员工祝您阖家幸福，生活愉快',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($pushUser->getWeiXin());
        $model->setTemplateId('cyqkZgKgGxJmZiqQnR8WGEhIrXMO13fqzsnGTOpbJQk');
        $model->run();
        return true;
    }

    /**
     * 推送给监理
     * @return bool
     * @throws BlException
     */
    public function pushAddOrder(){
//        dump((new GetRoleUser())->getEngineeringManager());
        $pushUser = new User((new GetRoleUser())->getEngineeringManager());
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }

        $firstValue = $pushUser->getNickName().'经理，有一条新工地需要您去分配监理';
        $keyword2 = $this->order->get_user_address();
        $keyword3 = $this->order->get_create_time();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword2,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>date('Y-m-d H:i:s',$keyword3),
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>$this->order->get_user_name(),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请尽快处理',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('rLgColDAcIxuRNHWAARs140WvNHtuB64Efe3L-g0g9E');
        $model->run();
        return true;
    }

    /**
     * 等待审核监理
     * @return bool
     * @throws BlException
     */
    public function waitModerated(){
        $pushUser = new User((new GetRoleUser())->getDeignDirector());
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }

        $firstValue = $pushUser->getNickName().',有一条新工地已指派好监理等待你的审核';
        $keyword1 = $this->order->get_order_number();
        $keyword2 = $this->order->get_user_name();
        $keyword3 = $this->order->get_user_address();
        $keyword4 = $this->order->get_create_time_str();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>$keyword2,
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>$keyword3,
                "color"=>"#173177",
            ],
            "keyword4"=>[
                'value'=>$keyword4,
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请尽快处理',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('-XaWPRzUkHKlYOUgVsPRzsclNCKYt7NS07Dprt-IZZU');
        $model->run();
        return true;
    }

    /**
     * 监理审核不通过
     * @return bool
     * @throws BlException
     */
    public function refuseModerated(){
        $pushUser = new User((new GetRoleUser())->getEngineeringManager());
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }

        $firstValue = $pushUser->getNickName().'经理,你指定的监理审核不通过';
        $keyword1 = $this->order->get_user_address();
        $keyword2 = '原因请登录系统查看';
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>$keyword2,
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请重新指派监理',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('c30-7UHoKsIJkwad87nQb8AtvpRk0wmrhttE2UQLGmA');
        $model->run();
        return true;
    }
    /**
     * 审核通过
     * @return bool
     * @throws BlException
     */
    public function agreeModerated(){
        $pushUser = $this->order->get_site_supervisor_user();
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }

        $firstValue = $pushUser->getNickName().'监理,你被指派负责新一个工地。';
        $keyword1 = $this->order->get_user_name();
        $keyword2 =$this->order->get_user_phone();
        $keyword3 =$this->order->get_user_address();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>$keyword2,
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>$keyword3,
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请查收',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('MSokwnBPpq_5jaBAc9UaCyoEJiTey4L_-uyHs86HxeA');
        $model->run();
        return true;
    }
    /**
     * 开工通知业主
     * @return bool
     * @throws BlException
     */
    public function startPush(){
        $phone =  $this->order->get_user_phone();
        $user   = new UserBase();
        $uid = $user->where([$user->_mobilephone=>$phone])->value($user->_id);
        $pushUser = new User($uid);
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }
        $startTime = date('Y-m-d H',$this->order->get_starting_time());
//        exception($this->order->get_starting_time_str());
        $firstValue = $pushUser->getNickName().'业主,您的工地开工吉日已选定于:'.$startTime;
        $keyword1 = $this->order->get_user_address();
//        $keyword2 =$this->order->get_user_phone();
//        $keyword3 =$this->order->get_user_address();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>'室内装修【开工仪式】',
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>'等待开工中',
                "color"=>"#173177",
            ],
            "keyword4"=>[
                'value'=>date('Y-m-d H:i'),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'阳新帝豪全体员工祝您阖家幸福，生活愉快',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('02Dw4ymdJF58tIApmpvMwVoDAZYbUE1R7WY8ooyxncQ');
        $model->run();
        return true;
    }

    /**
     * @return bool
     * @throws BlException
     */
    public function startPushInternal(){
        //监理
        $pushUser = $this->order->get_site_supervisor_user();
        $weiXin = $pushUser->getWeiXin();
        if($weiXin && $weiXin != '无'){
            $this->startPushSupervisor($weiXin,$pushUser);
        }
        //设计师
        $pushUser = $this->order->get_design_user();
        $weiXin = $pushUser->getWeiXin();
        if($weiXin && $weiXin != '无'){
            $this->startPushSupervisor($weiXin,$pushUser);
        }
        //工程部经理
        $pushUser = new User((new GetRoleUser())->getEngineeringManager());
        $weiXin = $pushUser->getWeiXin();
        if($weiXin && $weiXin != '无'){
            $this->startPushSupervisor($weiXin,$pushUser);
        }
        return true;
    }

    /**
     * @param $weiXin
     * @param $pushUser
     * @return bool
     * @throws BlException
     */
    private function startPushSupervisor($weiXin,$pushUser){

        $startTime = $this->order->get_starting_time_str();
//        exception($startTime);
        $firstValue = $pushUser->getNickName().',您的工地【'.$this->order->get_user_address().'】,将于:'.$startTime.'开工,请按时到场。';
        $keyword1 = $this->order->get_user_address();
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>'室内装修【开工仪式】',
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>'等待开工中',
                "color"=>"#173177",
            ],
            "keyword4"=>[
                'value'=>date('Y-m-d H:i'),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请注意查收',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('02Dw4ymdJF58tIApmpvMwVoDAZYbUE1R7WY8ooyxncQ');
        $model->run();
        return true;
    }

    /**
     * 工地日志等待回复
     * @return bool
     * @throws BlException
     */
    public function waitReply(){
        $pushUser = $this->order->get_site_supervisor_user();
        $weiXin = $pushUser->getWeiXin();
        if(empty($weiXin) || $weiXin == '无'){
            return true;
        }
        $keyword1 = $this->order->get_user_address();
        $firstValue = $pushUser->getNickName().'监理,你的工地【'.$keyword1.'】未提交工地日志,请尽快处理';

        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keynote1"=>[
                'value'=>$keyword1.',未提交工地日志',
                "color"=>"#173177",
            ],
            "keynote2"=>[
                'value'=>date('Y-m-d H:i:s',time()),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请尽快提交,谢谢配合',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setTemplateId('UlNrEyc1ElAOcanF2-e9ihiQ_-Vd99r6MnmmfX2qKy8');
        $model->run();
        return true;
    }

    /**
     * 推送工地日志
     * @return bool
     * @throws BlException
     */
    public function pushLog(){
        $phone =  $this->order->get_user_phone();
        $user   = new UserBase();
        $weiXin = $user->where([$user->_mobilephone=>$phone])->value($user->_id);
        $pushUser = new User($weiXin);
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }
        $firstValue = '尊敬的'.$pushUser->getNickName().'业主,您的工地有了新的进展，请您批阅,点击内容跳转：';
        $keyword1 = $this->order->get_user_address();

        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],

            "keyword1"=>[
                'value'=>$keyword1,
                "color"=>"#173177",
            ],
            "keyword2"=>[
                'value'=>'室内设计',
                "color"=>"#173177",
            ],
            "keyword3"=>[
                'value'=>'装修中',
                "color"=>"#173177",
            ],
            "keyword4"=>[
                'value'=>date('Y-m-d H:i'),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'请您批阅',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setUrl('http://admin.bangbangyz.com/Index/SiteLog/Index');
        $model->setTemplateId('02Dw4ymdJF58tIApmpvMwVoDAZYbUE1R7WY8ooyxncQ');
        $model->run();
        return true;
    }

    /**
     * 停工推送
     * @return bool
     * @throws BlException
     */
    public function stopWordPush(){
        $phone =  $this->order->get_user_phone();
        $user   = new UserBase();
        $weiXin = $user->where([$user->_mobilephone=>$phone])->value($user->_id);
        $pushUser = new User($weiXin);
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }
        $stopWord =  $this->order->getStopWordTag();
        $reasonContent = $this->order->getStopWordReason();
        $stopWordRemark = empty($reasonContent)?$stopWord->getTips():$reasonContent;
        $firstValue = '尊敬的'.$pushUser->getNickName().'业主,您的工地'.$this->order->get_user_address().'因:'.$stopWord->getTagName().'暂时停止施工';
        $remark = '请您尽快处理。带来不便之处请你见谅';
        if($stopWord->getTagId() == 2){
            $remark ='带来不便之处请你见谅';
        }
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],
            "keynote1"=>[
                'value'=>'详细情况：'.$stopWordRemark,
                "color"=>"#173177",
            ],
            "keynote2"=>[
                'value'=>date('Y-m-d H:i:s',time()),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>$remark,
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setUrl('http://admin.bangbangyz.com/Index/SiteLog/Index');
        $model->setTemplateId('LL3xctAjBMGio9sKhndyx4uvyX9INM5PNY1bZQiM4RQ');
        $model->run();
        return true;
    }

    /**
     * 标签取消推送
     * @return bool
     * @throws BlException
     */
    public function startWordPush(){
        $phone =  $this->order->get_user_phone();
        $user   = new UserBase();
        $weiXin = $user->where([$user->_mobilephone=>$phone])->value($user->_id);
        $pushUser = new User($weiXin);
        $weiXin = $pushUser->getWeiXin();
        if(!$weiXin || $weiXin == '无'){
            return true;
        }
        $firstValue = '尊敬的'.$pushUser->getNickName().'业主,您的工地'.$this->order->get_user_address().'已恢复正常施工';
        $data = [
            'first'=>[
                'value'=>$firstValue,
                "color"=>"#173177",
            ],
            "keynote1"=>[
                'value'=>'施工中',
                "color"=>"#173177",
            ],
            "keynote2"=>[
                'value'=>date('Y-m-d H:i:s',time()),
                "color"=>"#173177",
            ],
            'remark'=>[
                'value'=>'带来不便之处请你见谅。',
                "color"=>"#173177",
            ]
        ];
        $model = new WeiXinPush();
        $model->setData($data);
        $model->setTouser($weiXin);
        $model->setUrl('http://admin.bangbangyz.com/Index/SiteLog/Index');
        $model->setTemplateId('LL3xctAjBMGio9sKhndyx4uvyX9INM5PNY1bZQiM4RQ');
        $model->run();
        return true;
    }
}